"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MarkApi = void 0;
const dayjs_1 = __importDefault(require("dayjs"));
const utils_1 = require("../utils");
// import axios from "axios";
const axios = utils_1.proxyAxios();
class MarkApi {
    constructor(host, path) {
        this.host = host;
        this.path = path;
    }
    get uri() {
        return `${this.host}${this.path}`;
    }
    price() {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/price`);
            return data;
        });
    }
    priceParam(symbol) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/price`, { params: { 'symbol': symbol } });
            return data;
        });
    }
    priceParams(symbols) {
        return __awaiter(this, void 0, void 0, function* () {
            const map = new Map();
            let res = [];
            const price_data = yield this.price();
            for (const item of symbols) {
                price_data.filter(data => {
                    data.symbol == item ? res.push(data) : '';
                });
            }
            return res;
        });
    }
    exChangeInfo() {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/exchangeInfo`);
            return data;
        });
    }
    exChangeInfoParam(symbol) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/exchangeInfo`, { params: { 'symbol': symbol } });
            return data;
        });
    }
    exChangeInfoParams(symbols) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/exchangeInfo`, { params: { 'symbols': JSON.stringify(symbols) } });
            return data;
        });
    }
    depth(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, limit = 100 } = req;
            const { data } = yield axios.get(`${this.uri}/depth`, { params: { 'symbol': symbol, 'limit': limit } });
            return data;
        });
    }
    trades(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, limit = 500 } = req;
            const { data } = yield axios.get(`${this.uri}/trades`, { params: { 'symbol': symbol, 'limit': limit } });
            return data;
        });
    }
    aggTrades(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, limit = 500, fromId } = req;
            const { data } = yield axios.get(`${this.uri}/aggTrades`, { params: { 'symbol': symbol, 'limit': limit, 'fromId': fromId } });
            return data;
        });
    }
    aggTradesByTime(req) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, limit = 500, startTime, endTime } = req;
            req.limit = limit;
            const { data } = yield axios.get(`${this.uri}/aggTrades`, { params: req });
            return data;
        });
    }
    avgPriceParam(symbol) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/avgPrice`, { params: { 'symbol': symbol } });
            return data;
        });
    }
    tickerDaily() {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/24hr`);
            return data;
        });
    }
    tickerDailyParam(symbol) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/24hr`, { params: { 'symbol': symbol } });
            return data;
        });
    }
    tickerDailyParams(symbols) {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            const tickerData = yield this.tickerDaily();
            for (const item of symbols) {
                tickerData.filter(data => {
                    data.symbol == item ? arr.push(data) : '';
                });
            }
            return arr;
        });
    }
    bookTicker() {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/bookTicker`);
            return data;
        });
    }
    bookTickerParam(symbol) {
        return __awaiter(this, void 0, void 0, function* () {
            const { data } = yield axios.get(`${this.uri}/ticker/bookTicker`, { params: { 'symbol': symbol } });
            return data;
        });
    }
    bookTickerParams(symbols) {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            const bookData = yield this.bookTicker();
            for (const item of symbols) {
                bookData.filter(data => {
                    data.symbol == item ? arr.push(data) : '';
                });
            }
            return arr;
        });
    }
    klines(param) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, interval, limit = 500 } = param;
            const { data } = yield axios.get(`${this.uri}/klines`, { params: { 'symbol': symbol, 'interval': interval, 'limit': limit } });
            return data;
        });
    }
    klinesByTime(param) {
        return __awaiter(this, void 0, void 0, function* () {
            const { symbol, interval, startTime, endTime = dayjs_1.default().valueOf(), limit = 500 } = param;
            let res = yield axios.get(`${this.uri}/klines`, { params: { 'symbol': symbol, 'interval': interval, 'limit': limit, 'startTime': startTime, 'endTime': endTime } });
            const data = res.data;
            return data;
        });
    }
    kinesTime(param) {
        return __awaiter(this, void 0, void 0, function* () {
            const arr = [];
            const { symbol, interval, startTime, endTime = dayjs_1.default().valueOf(), limit = 500 } = param;
            let res = yield axios.get(`${this.uri}/klines`, { params: { 'symbol': symbol, 'interval': interval, 'limit': limit, 'startTime': startTime, 'endTime': endTime } });
            for (const item of res.data) {
                arr.push(item[0]);
            }
            return arr;
        });
    }
    klinesYear(param) {
        return __awaiter(this, void 0, void 0, function* () {
            let map = new Map();
            const { symbol, interval, startTime = 1629036800000, endTime = dayjs_1.default().valueOf() } = param;
            let { data } = yield axios.get(`${this.uri}/klines`, { params: { 'symbol': symbol, 'interval': '1M', 'limit': 1000, 'startTime': 0 } });
            for (const item of data) {
                const year = dayjs_1.default(item[0]).year();
                const old = map.get(year) || [0, 0, 0, 999999, 999999, 0];
                // console.log(old)
                const open = Math.max(Number(item[1]), Number(old[1]));
                const max = Math.max(Number(item[2]), Number(old[2]));
                const min = Math.min(Number(item[3]), Number(old[3]));
                const close = Math.min(Number(item[4]), Number(old[4]));
                const volume = Number(old[5]) + Number(item[5]);
                map.set(year, [year, open, max, min, close, volume]);
            }
            let arr = Array.from(map);
            console.log(arr);
            const start = dayjs_1.default(startTime).year();
            const end = dayjs_1.default(endTime).year();
            arr = arr.filter(item => {
                return item[0] == start || item[0] == endTime;
            });
            console.log(arr);
        });
    }
}
exports.MarkApi = MarkApi;
